import {
    asyncRouterMap,
    constantRouterMap,
    notExitMap,
    enterpriseRouterMap
} from '@/router'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
// function hasPermission (roles, route) {
//   if (route.meta && route.meta.roles) {
//     return true // roles.some(role => route.meta.roles.indexOf(role) >= 0)
//   } else {
//     return true
//   }
// }

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(asyncRouterMap, owerMenus) {
    const lastMenus = []
    for (let item of owerMenus) {
        for (let i of asyncRouterMap) {
            if (item.name && item.name === i.name) {
                var that = Object.assign({}, item, i)
                if (item.meta) {
                    that.meta.title = item.meta.title
                    that.meta.icon = i.meta.icon ? i.meta.icon : null
                }
                that.path = item.path
                if (that.children && that.children.length) {
                    that.children = filterAsyncRouter(i.children, item.children)
                }
                lastMenus.push(that)
            }
        }
    }
    return lastMenus
}

/**
 * 递归过滤异步路由表，返回符合用户产品权限的路由表
 * @param asyncRouterMap
 * @param ranges
 */
function filterRangesRouter(asyncRouterMap, ranges) {
    let finalMenus = []
    for (let i of asyncRouterMap) {
        let x = {};
        Object.assign(x, i);
        if (i.meta.roles) {//过滤未定义的产品范围组
            if (ranges.indexOf(i.meta.roles[0]) === -1) continue;
        }
        if (x.children && x.children.length) {
            x.children = filterRangesRouter(i.children, ranges)
        }
        finalMenus.push(x);
    }
    return finalMenus
}

const permission = {
    state: {
        routers: constantRouterMap,
        addRouters: []
    },
    mutations: {
        SET_ROUTERS: (state, routers) => {
            state.addRouters = routers
            state.routers = constantRouterMap.concat(routers)
        }
    },
    actions: {
        GenerateRoutes({commit}, data) {
            return new Promise(resolve => {
                const {userData} = data
                console.log('userData', userData)
                let accessedRouters
                accessedRouters = asyncRouterMap
                //过滤并生成自己的权限路由表关键代码
                // if (userData.roles.indexOf('监管') >= 0) {
                //     accessedRouters = asyncRouterMap //不过滤任何权限直接开放
                    // accessedRouters = filterRangesRouter(asyncRouterMap, userData.ranges)//过滤产品权限
                // } else {
                    // accessedRouters = filterAsyncRouter(asyncRouterMap, userData.owerMenus)//主逻辑生成自己的路由
                    // accessedRouters = enterpriseRouterMap//企业账户的路由
                // }
                //过滤代码结束
                accessedRouters = accessedRouters.concat(notExitMap)//concat() 方法用于连接两个或多个数组
                console.log('accessedRouters xxxx', accessedRouters)
                commit('SET_ROUTERS', accessedRouters)
                resolve()
            })
        }
    }
}

export default permission
